import logging

import pytest

from resource.pom.calculator import Calculator
from utils.manager import Images


class Test:
    app = Calculator

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        for i in range(5):
            logging.info('步骤1.{}：进入计算器应用'.format(i))
            device.start_ability(self.app.bundle_name, self.app.ability_name)
            if i == 0:
                logging.info('预期结果1.0：有权限弹窗，切弹窗显示正常')
                win_name, _ = device.get_focus_window()
                device.assert_current_window('permissionDialog')
                device.refresh_layout()
                dialog = device.get_element_by_type('JsView')
                dialog_area = device.get_element_area(dialog)
                dialog_snapshot = device.save_snapshot_to_local('permission_dialog.jpeg')
                Images.crop_picture(dialog_snapshot, *dialog_area)
                similarity = Images.compare_image_similarity(dialog_snapshot, self.app.standard_permission_dialog)
                assert similarity > 0.6, '弹窗显示异常'
                device.click_element(device.get_element_by_text('允许'))
            logging.info('预期结果1.{}：计算器打开无卡死，黑屏，白屏等现象'.format(i))
            device.assert_current_window(self.app.window_name)
            device.refresh_layout()
            device.assert_key_exist('expression')
            device.assert_key_exist('result')
            device.assert_key_exist('C')
            device.assert_key_exist('D')
            device.assert_key_exist('+')
            device.assert_key_exist('-')
            device.assert_key_exist('*')
            device.assert_key_exist('/')
            device.assert_key_exist('=')
            for i in range(10):
                device.assert_key_exist(str(i))
            device.go_back()
